﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Channels;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using OpenIDHostController.DataContract;
using ActionServices;
namespace OpenIDHostController
{
   public class OpenIDService:IOpenIDService
    {
        #region User

      
      public IList<usersContract> GetUserList(string Service, string UserName)
       {
           Helper.ValueDefine.Message =   "Get data: " + UserName + " has sent get all user command from service: " + Service + " at " + DateTime.Now;
           Helper.Log.AddLog(Helper.ValueDefine.Message);
           Helper.ValueDefine.LogMessage.Add(Helper.ValueDefine.Message);
           return usersContract.ToListConTract(RegisterService.RegisterServices.UserService.GetList());
       }


      public usersContract GetUserByID(long ID, string Service, string UserName)
       {
           Helper.ValueDefine.Message = "Get data: " + UserName + " has sent get user command with id="+ID+" from service: " + Service + " at " + DateTime.Now;
           Helper.Log.AddLog(Helper.ValueDefine.Message);
           Helper.ValueDefine.LogMessage.Add(Helper.ValueDefine.Message);
           return usersContract.ToContract(RegisterService.RegisterServices.UserService.GetByID(ID));
       }


      public usersContract GetUserByName(string Name, string Service, string UserName)
      {
          Helper.ValueDefine.Message = "Get data: " + UserName + " has sent get user command with Name=" + Name + " from service: " + Service + " at " + DateTime.Now;
          Helper.Log.AddLog(Helper.ValueDefine.Message);
          Helper.ValueDefine.LogMessage.Add(Helper.ValueDefine.Message);
           return usersContract.ToContract(RegisterService.RegisterServices.UserService.GetOneByLINQ(c=>c.fullname.Equals(Name)));
        }


      public usersContract GetUserByEmail(string Email, string Service, string UserName)
      {
          Helper.ValueDefine.Message = "Get data: " + UserName + " has sent get user command with Email=" + Email + " from service: " + Service + " at " + DateTime.Now;
          Helper.Log.AddLog(Helper.ValueDefine.Message);
          Helper.ValueDefine.LogMessage.Add(Helper.ValueDefine.Message);
           return usersContract.ToContract(RegisterService.RegisterServices.UserService.GetOneByLINQ(c=>c.email.Equals(Email)));
       }

      public usersContract GetUserByIndentityCard(string IndentityCard, string Service, string UserName)
       {
           Helper.ValueDefine.Message = "Get data: " + UserName + " has sent get user command with IndentityCard=" + IndentityCard + " from service: " + Service + " at " + DateTime.Now;
           Helper.Log.AddLog(Helper.ValueDefine.Message);
           Helper.ValueDefine.LogMessage.Add(Helper.ValueDefine.Message);
           return usersContract.ToContract(RegisterService.RegisterServices.UserService.GetOneByLINQ(c => c.indentitycard.Equals(IndentityCard)));
       }

      public usersContract GetUserByUserName(string Username, string Service, string UserName)
      {
          Helper.ValueDefine.Message = "Get data: " + UserName + " has sent get user command with Username=" + Username + " from service: " + Service + " at " + DateTime.Now;
          Helper.Log.AddLog(Helper.ValueDefine.Message);
          Helper.ValueDefine.LogMessage.Add(Helper.ValueDefine.Message);
          return usersContract.ToContract(RegisterService.RegisterServices.UserService.GetOneByLINQ(c => c.username == Username));
      }


        public usersContract GetUserByUserNameAndPassword(string Username, string password)
        {
            password = Helper.Security.EncryptString(password);
            return usersContract.ToContract(RegisterService.RegisterServices.UserService.GetOneByLINQ(c => c.username.Equals(Username)&c.password.Equals(password)));
        }

        public long SaveUser(out string error, string _object, string type, string service)//type: Register new user or Update user
        {

            var data = Helper.ConvertObject.ConvertJsonToObject<usersContract>(_object);
            Helper.ValueDefine.Message = type + ": " + data.username + ", from service: " + service + " at " + DateTime.Now;
            Helper.Log.AddLog(Helper.ValueDefine.Message);
            Helper.ValueDefine.LogMessage.Add(Helper.ValueDefine.Message);
            var checkusername = GetUserByUserName(data.username, service, data.username);
            if (checkusername != null)
            {
                error = "Tài khoản này đã tồn tại";
                return -1;
            }
            var checkemail = GetUserByEmail(data.email, service, data.username);
            if (checkemail != null)
            {
                error = "Email này đã tồn tại";
                return -1;
            }
            if (!String.IsNullOrEmpty(data.indentitycard))
            {
                var checkindentitycard = GetUserByIndentityCard(data.indentitycard, service, data.username);
                if (checkindentitycard != null)
                {
                    error = "Số CMND này đã tồn tại";
                    return -1;
                }
            }
            error = null;
            return RegisterService.RegisterServices.UserService.Save(usersContract.ToCoreData(data));
        }

       public bool DeleteUser(long UserID)
        {
            try
            {
                RegisterService.RegisterServices.UserService.Delete(RegisterService.RegisterServices.UserService.GetByID(UserID));
                RegisterService.RegisterServices.UserService.Delete(RegisterService.RegisterServices.UserGroupService.GetOneByLINQ(c => c.userid.Equals(UserID)));
                return true;
            }
            catch
            {
                return false;
            }
        }

        #endregion

        #region View user


        public IList<vwusersContract> GetViewUserList()
        {
            return vwusersContract.ToListConTract(RegisterService.RegisterServices.VwUserService.GetList());
        }

        public IList<vwusersContract> GetViewUserListByUserID(long UserID)
        {
           return vwusersContract.ToListConTract(RegisterService.RegisterServices.VwUserService.GetListByLINQ(c=>c.id==UserID));
        }


        public vwusersContract GetViewUserByUserID(long UserID)
        {
            return vwusersContract.ToContract(RegisterService.RegisterServices.VwUserService.GetByID(UserID));
        }

       /// <summary>
       /// Login return json data
       /// </summary>
       /// <param name="UserName"></param>
       /// <param name="Password"></param>
       /// <returns></returns>
        public string GetViewUserByUserNameAndPassword(string UserName, string Password, string Service)
        {
            string result  = null;
            try
            {
                Helper.ValueDefine.Message = "Login: " + UserName + " has sent login command from service: " + Service + " at " + DateTime.Now;
                Helper.Log.AddLog(Helper.ValueDefine.Message);
                Helper.ValueDefine.LogMessage.Add(Helper.ValueDefine.Message);
                Password = Helper.Security.EncryptString(Password);
                result = Helper.ConvertObject.ConvertObjectToJsonString(vwusersContract.ToListConTract(RegisterService.RegisterServices.VwUserService.GetListByLINQ(c => c.username.Equals(UserName) & c.password.Equals(Password))));          
                
            }
            catch
            {
                result = null;
            }
            if(!String.IsNullOrEmpty(result))
            {
                Helper.ValueDefine.Message = "Login: " + UserName + " has signed in to system from service: " + Service + " at " + DateTime.Now;
                Helper.Log.AddLog(Helper.ValueDefine.Message);
                Helper.ValueDefine.LogMessage.Add(Helper.ValueDefine.Message);
            }
            else
            {
                Helper.ValueDefine.Message = "Login: " + UserName + " can't login to system from service: " + Service + " at " + DateTime.Now;
                Helper.Log.AddLog(Helper.ValueDefine.Message);
                Helper.ValueDefine.LogMessage.Add(Helper.ValueDefine.Message);
            }
            return result;
            
        }

        #endregion


        #region user group

       
        public long SaveUserGroup(long UserID, long GroupID)
        {
            var data =  user_groupContract.ToCoreData(new user_groupContract(){ groupid = GroupID, userid =UserID});
            return RegisterService.RegisterServices.UserGroupService.Save(data);
        }



        #endregion

    }
}
